/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')
	
GBIF.GeoSpatial.WMSCapabilitiesLayers = function(config){	

	var indexI;
	var sm = new Ext.grid.CheckboxSelectionModel({
		checkOnly: true
	});

	var store = new GeoExt.data.WMSCapabilitiesStore({
		url: Config.General.Home.url + 'api/getcapabilities-proxy.php'
	});

	Ext.apply(this,config,{
			title: "WMS Capabilities"
		,	store: store
		,	sm: sm
		,	loadMask:true
		,	columns: [
					sm
				,	{header: "Title", dataIndex: "title", sortable: true}
				,	{header: "Name", dataIndex: "name", sortable: true, hidden:true}
				,	{header: "Queryable", dataIndex: "queryable", sortable: true, width: 70}
				,	{id: "description", header: "Description", dataIndex: "abstract"}
			]
		,	autoExpandColumn : 'description'
		,	tbar:[{
					text:'Add Layer'
				,	scope:this
				,	ref:'../addlayer'
				,	handler:this.layerData
			}]
		,	listeners: {
				rowcontextmenu: function(grid, index, e){
					indexI=index;
					if (!this.menu) {
						this.menu = new Ext.menu.Menu({
								id: 'menuId'
							,	items: [{
										text: 'Preview'
									, scope: this
									,	handler: function(){
											this.getSelectionModel().selectRow(indexI,true);
											var rowdata = this.getSelectionModel().getSelections();
											this.fireEvent('showWin',rowdata,this);
										}
								}]
						});
						this.menu.on('hide', this.onContextHide, this);
					}
					e.stopEvent();
			        
					this.menu.showAt(e.getXY());
			}
		}
	});
	
	GBIF.GeoSpatial.WMSCapabilitiesLayers.superclass.constructor.call(this, config);
}

Ext.extend(GBIF.GeoSpatial.WMSCapabilitiesLayers,Ext.grid.GridPanel, {
	
		layerData: function(){
			if (this.getSelectionModel().getSelections()) {
				var rowdata = this.getSelectionModel().getSelections();
				this.fireEvent('addLayer', rowdata,this);
			}
		}
	
	,	preview: function(grid, index, e) {
			grid.getSelectionModel().selectRow(index);
			var rowdata = this.getSelectionModel().getSelected();
			this.fireEvent('addLayer', rowdata,this); 
		}

});